package com.mall.shopping.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.commons.tool.exception.ValidateException;
import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.*;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @date: 2022-01-20-10-27
 * @author: ZYY
 * @description:
 */
@Service
public class IProductDetailServiceImp implements IProductService {

    @Autowired
    ItemMapper productMapper;

    @Autowired
    ProductConverter productConverter;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    PanelMapper panelMapper;

    @Autowired
    ContentConverter contentConverter;

    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse productDetailResponse = new ProductDetailResponse();
        try {
            // 参数校验
            request.requestCheck();

            // 查询数据库
            ItemDescItem item = productMapper.selectProductDetail(request);

            // 数据转换
            ProductDetailDto productDto =productConverter.itemDescItem2ProductDetail(item);

            // 执行成功
            productDetailResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            productDetailResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            productDetailResponse.setProductDetailDto(productDto);

        }catch(ValidateException e){
            e.printStackTrace();
            productDetailResponse.setCode(e.getErrorCode());
            productDetailResponse.setMsg(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            productDetailResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            productDetailResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }
        return productDetailResponse;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        AllProductResponse allProductResponse = new AllProductResponse();
        try {
            // 参数校验
            request.requestCheck();

            // 查询数据库
            // 开启pageHelper
            Integer page = request.getPage();
            Integer size = request.getSize();
            PageHelper.startPage(page,size);
            List<Item> items = productMapper.selectItemFront(request.getCid(),"price",
                    request.getSort(),request.getPriceGt(),request.getPriceLte());
            PageInfo<Item> pageInfo = new PageInfo<>(items);
            long total = pageInfo.getTotal();

            // 数据转换
            List<ProductDto> productDtos = productConverter.items2Dto(items);

            // 封装结果
            allProductResponse.setTotal(total);
            allProductResponse.setProductDtoList(productDtos);
            allProductResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            allProductResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        }catch(ValidateException e){
            e.printStackTrace();
            allProductResponse.setCode(e.getErrorCode());
            allProductResponse.setMsg(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            allProductResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            allProductResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }
        return allProductResponse;
    }

    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse response = new RecommendResponse();
        try {
            // 获得参数并校验
            // 查询数据库
            // 1 查询到属于热门推荐记录,并转化为对应的Dto
            List<Panel> panels = panelMapper.selectPanelContentById(6);
            List<PanelDto> panelDtos = contentConverter.panel2Dto(panels);
            // 2 根据热门记录id查询对应的热门商品
            for (int i = 0; i < panels.size(); i++) {
                // 根据热门记录的id查询对应的板块内容
                List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panels.get(i).getId());
                // 数据转化
                List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
                panelDtos.get(i).setPanelContentItems(panelContentItemDtos);
            }


            // 返回输出结果
            Set<PanelDto> panelDtosSet = new HashSet<>(panelDtos);
            response.setPanelContentItemDtos(new HashSet<>(panelDtosSet));
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            response.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            response.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }
        return response;
    }
}